package org.study.swing.study01.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月14日
 */
public class PanelDemo1 {
    public static void main(String[] args) {
        testPanel1();
    }

    public static void testPanel1() {
        JFrame frame = new JFrame();
        frame.setTitle("示例：面板");

        // 获取默认窗格
        Container contentPane = frame.getContentPane();
        JPanel panel1 = new JPanel();
        // 设置面板1的背景颜色为绿色
        panel1.setBackground(Color.PINK);
        JPanel panel2 = new JPanel();
        JButton button1 = new JButton("登录");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String actionCommand = event.getActionCommand();
                Object source = event.getSource();
                if (source instanceof JButton) {
                    JButton button = (JButton) source;
                    String text = button.getText();
                    System.out.println("按钮被点击了：" + text);
                }
            }
        });
        JButton button2 = new JButton("退出");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String actionCommand = event.getActionCommand();
                Object source = event.getSource();
                if (source instanceof JButton) {
                    System.exit(0);
                }
            }
        });
        // 将按钮1放到面板2中
        panel2.add(button1);
        // 将按钮2放到面板2中
        panel2.add(button2);
        // 将面板1放到窗口中部
        contentPane.add(panel1, BorderLayout.CENTER);
        // 将面板2放到窗口下部
        contentPane.add(panel2, BorderLayout.SOUTH);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
